﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.Models;
using SYcwpb.ViewModels;
using SYcwpb.Access;
using System.Windows.Media.Animation;
using SYcwpb.Helpers;
using System.Data;

namespace SYcwpb.Views
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : Window
    {
        MainViewModel _viewModel;

        GlobalConfigurationAccess _access = new GlobalConfigurationAccess();

        public MainView()
        {
                //控制wpf动画不那么耗费cpu
                Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 });
                InitializeComponent();
                //this.grpboxDutyInfo.Template = this.Resources["singleDriverTemplate"] as ControlTemplate;
                _viewModel = new MainViewModel(this);
                this.DataContext = _viewModel;
            
        }

        public void VoiceButtonChangeTemplate(bool isVoiceOpen)
        {
            if (isVoiceOpen)
            {
                this.btn_voice.Template = this.Resources["voiceClose"] as ControlTemplate;
            }
            else
                this.btn_voice.Template = this.Resources["voiceOpen"] as ControlTemplate;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _viewModel.Dispose();
            Application.Current.Shutdown();
            
            
        }


        public List<Driver> list;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //初始化需要从数据库读取的全局静态变量
            _access.InitDelayInterval();

            _viewModel.Load();
           // this.WindowState = WindowState.Normal;
            this.Title ="沈阳乘务排班"+ _viewModel.GetVersion();
            this.WindowState = WindowState.Maximized;


            list = InitDriverComboBox();
            cbbdriver.ItemsSource = list;
            cbbdriver.SelectedValuePath = "userName";
            cbbdriver.DisplayMemberPath = "userViewNameAndUserName";
        }

        private void cancelShow_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //DataRow row = dg_reminding.SelectedItem as DataRow;
                
                Hyperlink link = sender as Hyperlink;
                object obj = link.Tag;
                if (obj != null)
                {
                    int id=Convert.ToInt32(obj);
                    if (_viewModel.CalcelShow(id))
                    {
                        if(dg_reminding.Tag!=null){
                            DataTable dt = dg_reminding.Tag as DataTable;

                            for (int i = 0; i < dt.Rows.Count;i++ )
                            {
                                if (Convert.ToInt32(dt.Rows[i]["Idd"]) == id)
                                    dt.Rows.RemoveAt(i);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        public List<Driver> InitDriverComboBox()
        {
            List<Driver> lstDrive = new List<Driver>();
            SqlDataLayer sqlHelper = new SqlDataLayer();
            int line = Convert.ToInt32(CONSTDATA.UserModel.Line);
            string strSql = "select user_id, user_name,user_view_name,(user_view_name+' '+user_name) as abc from [system_user] where user_dep_id =7 and user_line = " + line + " order by user_view_name";
            DataTable driverDT = sqlHelper.SelectDt(strSql);
            if (driverDT.Rows.Count > 0)
            {
                for (int i = 0; i < driverDT.Rows.Count; i++)
                {
                    Driver driver = new Driver();
                    driver.userName = driverDT.Rows[i]["user_name"].ToString();
                    driver.userViewName = driverDT.Rows[i]["user_view_name"].ToString();
                    driver.userid = driverDT.Rows[i]["user_id"].ToString();
                    driver.userViewNameAndUserName = driverDT.Rows[i]["abc"].ToString();
                    //string item = driver.userViewName + " " + driver.userName;
                    lstDrive.Add(driver);
                }
            }
            return lstDrive;
        }
    }
}
